package com.singfan.script;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.singfan.common.nosql.mongo.MongoContext;
import com.singfan.core.dao.database.IShopHairServiceDao;
import com.singfan.core.dao.database.model.ShopHairService;
import com.singfan.core.dao.database.model.constant.HairServiceType;
import com.singfan.script.importdata.BarberSheetParser;
import com.singfan.script.importdata.HairstyleSheetParser;
import com.singfan.script.importdata.SheetParser;
import com.singfan.script.importdata.ShopSheetParser;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.FileInputStream;
import java.util.*;

/**
 * 数据导入脚本类
 * Author: hansonghe
 * Date: 2015-08-19 15:27
 * Desc:
 */
@Component
public class DataImportScript extends ScriptRunner {
    @Autowired
    private IShopHairServiceDao shopHairServiceDao;

    @Override
    public void run(List<String> args) {
        String excelFilePath = args.get(0);
        LinkedHashMap<String, SheetParser> sheetParserMap = new LinkedHashMap<>();

        sheetParserMap.put("门店信息", ScriptContext.getSpringContext().getBean(ShopSheetParser.class));
        sheetParserMap.put("发型师信息", ScriptContext.getSpringContext().getBean(BarberSheetParser.class));
        sheetParserMap.put("发型师作品信息", ScriptContext.getSpringContext().getBean(HairstyleSheetParser.class));

        try {
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(excelFilePath));
            Set<String> sheetNameSet = sheetParserMap.keySet();
            for (String sheetName : sheetNameSet){
                XSSFSheet sheet = workbook.getSheet(sheetName);
                sheetParserMap.get(sheetName).parse(sheet);
            }
            parseService();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void parseService(){
        MongoClient mongoClient = MongoContext.getMongoClientProvider().getMongoClient();
        MongoDatabase database = mongoClient.getDatabase("singfan_location_0");

        MongoCollection<Document> collection = database.getCollection("service");
        MongoCursor<Document> cursor = collection.find().iterator();
        List<ShopHairService> shopHairServiceList = new ArrayList<>();
        while (cursor.hasNext()) {
            Document document = cursor.next();
            int shopId = document.getInteger("shopid");
            ArrayList farelistDocumentList = (ArrayList)document.get("farelist");
            for (Object fareObj : farelistDocumentList){
                Document fareDocument = (Document)fareObj;
                String name = fareDocument.getString("name");
                if (name.equals("剪发") || name.equals("造型")){
                    ShopHairService shopHairService = createShopHairService(shopId, getServiceType(name), name, fareDocument);
                    shopHairServiceList.add(shopHairService);
                }
                else {
                    int serviceType = getServiceType(name);
                    ArrayList itemsList = (ArrayList)fareDocument.get("items");
                    for (Object itemObj : itemsList){
                        Document itemDocument = (Document)itemObj;
                        name = itemDocument.getString("name");
                        ShopHairService shopHairService = createShopHairService(shopId, serviceType, name, itemDocument);
                        shopHairServiceList.add(shopHairService);
                    }
                }
            }
        }

        for (ShopHairService shopHairService : shopHairServiceList){
            int shopHairServiceId = shopHairServiceDao.create(shopHairService);
        }
    }

    private ShopHairService createShopHairService(long shopId, int serviceType, String serviceName, Document fareDocument){
        ShopHairService shopHairService = new ShopHairService();
        shopHairService.setShopId(shopId);
        shopHairService.setServiceName(serviceName);
        shopHairService.setServiceType(serviceType);
        shopHairService.setOriginalPrice(fareDocument.getInteger("sale"));
        shopHairService.setRealPrice(fareDocument.getInteger("up"));
        shopHairService.setCreateTime(new Date());
        return shopHairService;
    }

    private int getServiceType(String serviceName){
        int serviceType = 0;
        if (serviceName.equals("剪发")){
            serviceType = HairServiceType.HAIR_CUT;
        }
        else if (serviceName.equals("烫发")){
            serviceType = HairServiceType.HAIR_PERM;
        }
        else if (serviceName.equals("染发")){
            serviceType = HairServiceType.HAIR_DYE;
        }
        else if (serviceName.equals("护理")){
            serviceType = HairServiceType.HAIR_NURSE;
        }
        else if (serviceName.equals("造型")){
            serviceType = HairServiceType.HAIR_MODELING;
        }

        return serviceType;
    }
}
